/*
 * CS_GET_NEW_MAIL_COUNT.cpp
 *
 *  Created on: 2013. 8. 19.
 *      Author: the2
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/MailBox.h"
#include "../../NLogic/Static.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_GET_NEW_MAIL_COUNT(User * pUser, ::Network::Packet * pPacket)
{
/*	CS_GET_NEW_MAIL_COUNT				0x120A
	{
	}

	SC_GET_NEW_MAIL_COUNT				0x120B
	{
		s16				s16ack;
		u8				u8mail_count;
	}*/

	PREPARE_PACKET;

	NLogic::MailBox * pMailBox	= pUser->GetMailBox();
	if (NULL == pMailBox)
	{
		DEBUGLOG("CS_GET_NEW_MAIL_COUNT : pMailBox is NULL!!");
		return;
	}

	// 3일이 지난 데이터는 가져오지 않는다.
	time_t criterionSec = time(NULL) - (3 * 3600 * 24);

	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM data_mail WHERE recv_user_seq = %d AND mail_status <> 2 AND send_date >= %lld",
			pUser->GetUserSeq(),
			criterionSec);

	if (false == ::Database::GETSESSION()->SyncExecute(pUser->GetUserInfo().shard_Item_db_type, clsAuto))
	{
		DEBUGLOG("CS_GET_NEW_MAIL_COUNT : false == ::Database::GETSESSION()->SyncExecute(1, clsAuto)");
		return;
	}

	if (clsAuto->GetErrorIdx() != 0)
	{
		DEBUGLOG("CS_GET_NEW_MAIL_COUNT : clsAuto->GetErrorIdx() != 0");
		return;
	}

	// 메일트리 초기화
	pMailBox->Finalize();

	int newMail	= 0;

	while( clsAuto->Next() )
	{
		uint64_t mailSeq;
		NLogic::MailBox::Mail::Data clsData;

		clsAuto->GetRecord() >> mailSeq;
		clsAuto->GetRecord() >> clsData.send_user_seq;
		clsAuto->GetRecord() >> clsData.send_user_nickname;
		clsAuto->GetRecord() >> clsData.send_user_imageurl;
		clsAuto->GetRecord() >> clsData.recv_user_seq;
		clsAuto->GetRecord() >> clsData.mail_status;
		clsAuto->GetRecord() >> clsData.gift_status;
		clsAuto->GetRecord() >> clsData.gift_vcindex;
		clsAuto->GetRecord() >> clsData.zen;
		clsAuto->GetRecord() >> clsData.gold;

		clsAuto->GetRecord() >> clsData.item.bag_type;
		clsAuto->GetRecord() >> clsData.item.bag_slot_number;
		clsAuto->GetRecord() >> clsData.item.quantity;
		clsAuto->GetRecord() >> clsData.item.class_type;
		clsAuto->GetRecord() >> clsData.item.sub_type;
		clsAuto->GetRecord() >> clsData.item.tid;
		clsAuto->GetRecord() >> clsData.item.iconidx;
		clsAuto->GetRecord() >> clsData.item.item_type;
		clsAuto->GetRecord() >> clsData.item.cur_duration;
		clsAuto->GetRecord() >> clsData.item.max_duration;
		clsAuto->GetRecord() >> clsData.item.set_type;
		clsAuto->GetRecord() >> clsData.item.non_identity;
		clsAuto->GetRecord() >> clsData.item.cur_refine_step;
		clsAuto->GetRecord() >> clsData.item.quality;
		clsAuto->GetRecord() >> clsData.item.level;

		for (int i = 0; i < 7; ++i)
		{
			clsAuto->GetRecord() >> clsData.item.eff_type[i];
			clsAuto->GetRecord() >> clsData.item.eff_pos[i];
			clsAuto->GetRecord() >> clsData.item.eff_value[i];
		}

		clsAuto->GetRecord() >> clsData.item.open_upgrade_stone_slot;
		clsAuto->GetRecord() >> clsData.item.ability_enhance_rate;
		clsAuto->GetRecord() >> clsData.item.max_enhance_step;
		clsAuto->GetRecord() >> clsData.item.buy_use;
		clsAuto->GetRecord() >> clsData.item.evolve_step;
		clsAuto->GetRecord() >> clsData.item.evolve_max;
		clsAuto->GetRecord() >> clsData.item.evolve_point;
		clsAuto->GetRecord() >> clsData.item.evolve_percent;
		clsAuto->GetRecord() >> clsData.item.evolve_value;

		clsAuto->GetRecord() >> clsData.message;
		clsAuto->GetRecord() >> clsData.send_date;

		NLogic::MailBox::Mail * pMail = NLogic::Static::InstancePtr()->GETMAILPOOL()->NEW();
		pMail->Open_FromDatabase(mailSeq, clsData, pUser);
		pMailBox->AddMailData(pMail);

		if (0 == clsData.mail_status)
			newMail++;
	}

	::Network::Packet * pAck	= ::Network::GETPACKETPOOL()->SEND_NEW();

	*pAck << static_cast<int16_t>(ACK_OK);
	*pAck << static_cast<int8_t>(newMail);

	pAck->MakePacket(CMD_SC_GET_NEW_MAIL_COUNT);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/

